package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportUserCertificateRpcService;
import com.hunttown.mes.rpc.domain.TransportUserCertificateDTO;
import com.hunttown.mes.rpc.domain.query.TransportUserCertificateDTOQuery;
import com.hunttown.mes.service.TransportUserCertificateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportUserCertificateConverts.convert;

/**
 * created by wangjunfu on 2022-12-04 11:33:05
 */
@Service
public class TransportUserCertificateRpcServiceImpl implements TransportUserCertificateRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportUserCertificateRpcServiceImpl.class);

    private final TransportUserCertificateService transportUserCertificateService;

    @Autowired
    public TransportUserCertificateRpcServiceImpl(TransportUserCertificateService transportUserCertificateService) {
        this.transportUserCertificateService = transportUserCertificateService;
    }

    @Override
    public TransportUserCertificateDTO insert(TransportUserCertificateDTO transportUserCertificateDTO) {
        try {
            return convert(transportUserCertificateService.insert(convert(transportUserCertificateDTO)));
        } catch (Exception e) {
            logger.error("TransportUserCertificate create failed.", e);
            throw new RuntimeException("TransportUserCertificate create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportUserCertificateDTO transportUserCertificateDTO) {
        try {
            return transportUserCertificateService.updateInfoById(convert(transportUserCertificateDTO));
        } catch (Exception e) {
            logger.error("TransportUserCertificate update by id failed.", e);
            throw new RuntimeException("TransportUserCertificate update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportUserCertificateDTOQuery transportUserCertificateDTOQuery) {
        try {
            Query query = Query.noPage();

            if (transportUserCertificateDTOQuery.getW_id() != null && transportUserCertificateDTOQuery.getW_id() > 0) {
                query.put("w_id", transportUserCertificateDTOQuery.getW_id());
            }

            if (transportUserCertificateDTOQuery.getIds() != null && !transportUserCertificateDTOQuery.getIds().equals("")) {
                query.put("ids", transportUserCertificateDTOQuery.getIds());
            }

            if (transportUserCertificateDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCertificateDTOQuery.getStopFlag());
            }

            if (transportUserCertificateDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCertificateDTOQuery.getDeleteFlag());
            }

            return transportUserCertificateService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportUserCertificate update by query failed.", e);
            throw new RuntimeException("TransportUserCertificate update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCertificateDTO selectById(Integer id) {
        try {
            return convert(transportUserCertificateService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportUserCertificate select by id failed.", e);
            throw new RuntimeException("TransportUserCertificate select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCertificateDTO selectObjByQuery(TransportUserCertificateDTOQuery transportUserCertificateDTOQuery) {
        try {
            Query query = Query.noPage();

            if (transportUserCertificateDTOQuery.getPositionName() != null) {
                query.put("positionName", transportUserCertificateDTOQuery.getPositionName());
            }

            if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getTrueName())) {
                query.put("trueName", transportUserCertificateDTOQuery.getTrueName());
            }

            if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getCertificateNo())) {
                query.put("certificateNo", transportUserCertificateDTOQuery.getCertificateNo());
            }

            if (transportUserCertificateDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCertificateDTOQuery.getStopFlag());
            }

            if (transportUserCertificateDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCertificateDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getOrderBy())) {
                query.put("orderBy", transportUserCertificateDTOQuery.getOrderBy());
            }

            return convert(transportUserCertificateService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportUserCertificate select obj by query failed.", e);
            throw new RuntimeException("TransportUserCertificate select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportUserCertificateDTO> getListForPage(TransportUserCertificateDTOQuery transportUserCertificateDTOQuery) {
        Query query = Query.page(transportUserCertificateDTOQuery.getPn(), transportUserCertificateDTOQuery.getPageSize());

        if (transportUserCertificateDTOQuery.getId() != null && transportUserCertificateDTOQuery.getId() > 0) {
            query.put("id", transportUserCertificateDTOQuery.getId());
        }

        if (transportUserCertificateDTOQuery.getIds() != null && !transportUserCertificateDTOQuery.getIds().equals("")) {
            query.put("ids", transportUserCertificateDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getLikeName())) {
            query.put("likeName", transportUserCertificateDTOQuery.getLikeName());
        }

        if (transportUserCertificateDTOQuery.getUserId() != null && transportUserCertificateDTOQuery.getUserId() > 0) {
            query.put("userId", transportUserCertificateDTOQuery.getUserId());
        }

        if (transportUserCertificateDTOQuery.getPositionName() != null && transportUserCertificateDTOQuery.getPositionName() > 0) {
            query.put("positionName", transportUserCertificateDTOQuery.getPositionName());
        }

        if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getShowDate())) {
            query.put("showDate", transportUserCertificateDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getStartDate())) {
            query.put("startDate", transportUserCertificateDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getEndDate())) {
            query.put("endDate", transportUserCertificateDTOQuery.getEndDate());
        }

        if (transportUserCertificateDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportUserCertificateDTOQuery.getStopFlag());
        }

        if (transportUserCertificateDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportUserCertificateDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportUserCertificateDTOQuery.getOrderBy())) {
            query.put("orderBy", transportUserCertificateDTOQuery.getOrderBy());
        }

        List<TransportUserCertificateDTO> list = convert(transportUserCertificateService.selectByQuery(query));
        Page<TransportUserCertificateDTO> page = Page.from(transportUserCertificateDTOQuery.getPn(), transportUserCertificateDTOQuery.getPageSize(), list);
        page.setTotalItems(transportUserCertificateService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportUserCertificateService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportUserCertificate delete failed.", e);
            throw new RuntimeException("TransportUserCertificate delete failed, Please order your code.", e);
        }
    }
}